<template>
  <div>
        <Card>
            <div slot="title" >
                <Button v-if="btnp.plxzeng_xzsb" @click="modal_btadd=true" class="zh-hollow" type="primary" icon="plus-round">批量新增设备</Button>
            </div>
            <Form :model="formItem" :label-width="70">
                <Row :gutter="10" type="flex"  align="middle"  >
                    <Col span="8">
                        <FormItem label="设备">
                            <Input v-model="formItem.code" placeholder="设备编号" clearable></Input>
                        </FormItem>
                    </Col>
                    <Col span="8">
                        <FormItem label="类型">
                            <Cascader  :data="moreTypeList" v-model="formItem.type" placeholder="请选择父类子类"></Cascader>
                        </FormItem>
                    </Col>
                    <Col span="8" v-if="user_type!='01'">
                        <FormItem label="所属客户">
                            <Select v-model="formItem.customerUuid" filterable @on-change="searchCustomerChange">
                                <Option  v-for="item in moreCustomerList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                            </Select>
                        </FormItem>
                    </Col> 
                    <Col span="8">
                        <FormItem label="机构">
                            <Input v-if="user_type!='01'" @on-focus="s_tree=true;" :disabled="formItem.customerUuid.length<=0||formItem.customerUuid=='全部'" v-model="formItem.instName" placeholder="请选择机构" readonly></Input>
                            <Input v-else @on-focus="s_tree=true;"  v-model="formItem.instName" placeholder="请选择机构" readonly></Input>
                            <!-- <Select v-if="user_type!='01'" v-model="formItem.inst" filterable :disabled="formItem.customerUuid=='全部'||formItem.customerUuid.length<=0">
                                <Option  v-for="item in moreMechanismList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                            </Select>
                            <Select v-else v-model="formItem.inst" filterable>
                                <Option  v-for="item in moreMechanismList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                            </Select> -->
                        </FormItem>
                    </Col>                                       
                    <Col span="12">
                        <FormItem label="新增时间">
                            <DatePicker v-model="timeRange" format="yyyy-MM-dd HH:mm" type="datetimerange" placement="bottom-end" placeholder="请选择开始和结束时间"></DatePicker>
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="10" >
                    <Col span="8" offset="16"  style="text-align:right;">
                        <Button @click='searchGetTable' class="zh-hollow" icon="ios-search-strong" type="primary" >搜索</Button>
                    </Col>
                </Row>
            </Form>
        </Card>
        <Card style="margin-top:10px;" :padding="0">
            <Table :loading="loading" border :columns="columns" :data="data"></Table>
            <Page :current.sync="formItem.pageNumber"  :total="total" show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
        </Card>
        <Modal  title="设备二维码" v-model="modal_qr"  class-name="vertical-center-modal">
            <div style="padding:15px 30px;textAlign:center" >
                <img style="display:block;width:180px;height:180px;margin: 0 auto;" :src="qrcodeinfo.qrcode" alt="">
            </div>
            <div slot="footer">
                <div style="textAlign:center;width:100%;">
                    <!-- <a :href="qrcodeinfo.qrcode" :download="qrcodeinfo.name+'.jpg'" target="_blank">
                        <Button  class="zh-hollow" type="primary" icon="ios-download-outline">下载</Button>
                    </a> -->
                    <Button  @click="downQrPic" class="zh-hollow" type="primary" icon="ios-download-outline">
                        下载
                    </Button>
                </div>
                
            </div>
        </Modal>
        <Modal 
            title="编辑设备信息" 
            v-model="modal_deviceInfo"  
            ok-text="保存" 
            cancel-text="放弃操作"
            :loading='saveLoad'
            @on-ok="validate"
        >
            <Form style="padding:15px 30px;"  :model="deviceInfo" :label-width="90">
                <FormItem label="所属客户*" v-if="user_type!='01'">
                    <Select v-if="modal_deviceInfo" v-model="deviceInfo.customerUuid" filterable @on-change='formCustomerChange'>
                        <Option  v-for="item in customerList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属机构*">
                    
                    <!-- <Select @on-change="changeInst" v-if="user_type!=='01'" :disabled="deviceInfo.customerUuid.length<=0" v-model="deviceInfo.instUuid" filterable>
                        <Option  v-for="item in mechanismList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select>
                    <Select @on-change="changeInst" v-else  v-model="deviceInfo.instUuid" filterable>
                        <Option  v-for="item in mechanismList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select> -->
                    <Input v-if="user_type!=='01'" @on-focus="f_tree=true;" :disabled="deviceInfo.customerUuid==undefined||deviceInfo.customerUuid==''" v-model="deviceInfo.inst_name" placeholder="请选择机构" readonly></Input>
					<Input v-else @on-focus="f_tree=true;"  v-model="deviceInfo.inst_name" placeholder="请选择机构" readonly></Input>
                </FormItem>
                <FormItem label="所在部门">
                    <Select v-model="deviceInfo.deptUuid" filterable :disabled="deviceInfo.instUuid.length<=0">
                        <Option  v-for="item in partList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="设备类型*">
                    <Cascader :data="typeList" v-model="deviceInfo.type"></Cascader>
                </FormItem>
                <FormItem label="设备名称*">
                    <Input v-model="deviceInfo.name" clearable></Input>
                </FormItem>
                <FormItem label="品牌">
                    <Input v-model="deviceInfo.brand" clearable></Input>
                </FormItem>
                <FormItem label="采购日期">
                    <DatePicker  :value="deviceInfo.date"  type="date" @on-change="changeDate" ></DatePicker>
                </FormItem>
                <FormItem label="采购金额">
                    <Input v-model="deviceInfo.amt" @on-keyup="amtChange" :maxlength="9" clearable></Input>
                </FormItem>
                <FormItem v-if="!deviceInfo.more"  >
                    <Button type="text" @click="deviceInfo.more=true" icon="ios-arrow-thin-down" >更多信息</Button>
                </FormItem>
                <template v-if="deviceInfo.more">
                    <FormItem label="设备序列号">
                        <Input v-model="deviceInfo.sn" clearable></Input>
                    </FormItem>
                    <FormItem label="资产编号">
                        <Input v-model="deviceInfo.assets_code" clearable></Input>
                    </FormItem>
                    <FormItem label="领用人">
                        <Input v-model="deviceInfo.receiptor" clearable></Input>
                    </FormItem>
                    <FormItem label="领用时间">
                        <DatePicker :value="deviceInfo.dateTime" type="datetime" format="yyyy-MM-dd HH:mm" @on-change="changeDateTime"  ></DatePicker>
                    </FormItem>
                    <FormItem label="IP地址">
                        <Input v-model="deviceInfo.ip" clearable></Input>
                    </FormItem>
                    <FormItem label="mac地址">
                        <Input v-model="deviceInfo.mac" clearable></Input>
                    </FormItem>
                    <FormItem label="厂商支持人">
                        <Input v-model="deviceInfo.facilitator" clearable></Input>
                    </FormItem>
                    <FormItem label="厂商支持电话">
                        <Input v-model="deviceInfo.facilitator_tel" clearable></Input>
                    </FormItem>
                    <FormItem label="备注">
                        <Input v-model="deviceInfo.remarks" clearable></Input>
                    </FormItem>
                    <FormItem   >
                        <Button type="text" @click="deviceInfo.more=false" icon="ios-arrow-thin-up" >收起信息</Button>
                    </FormItem>
                </template>
            </Form>
        </Modal>
        <Modal
            title="批量新增设备" 
            v-model="modal_btadd" 
        >
            <Form style="padding:15px 30px;"  :model="btadd" :label-width="90">
                <FormItem label="所属客户" v-if="user_type!='01'">
                    <Select v-if="modal_btadd" v-model="btadd.customer" filterable @on-change='formCustomerChange2'>
                        <Option  v-for="item in customerList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="所属机构">
                    <!-- <Select  v-if="user_type!=='01'" :disabled="btadd.customer.length<=0" v-model="btadd.mechanism" filterable>
                        <Option  v-for="item in mechanismList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select>
                    <Select  v-else  v-model="btadd.mechanism" filterable>
                        <Option  v-for="item in mechanismList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                    </Select> -->
                    <Input v-if="user_type!=='01'" @on-focus="f_tree=true;" :disabled="btadd.customer==''||btadd.customer==undefined" v-model="btadd.mechanismName" placeholder="请选择机构" readonly></Input>
					<Input v-else @on-focus="f_tree=true;"  v-model="btadd.mechanismName" placeholder="请选择机构" readonly></Input>
                </FormItem>
                <FormItem label="设备类型">
                    <Cascader :data="typeList" v-model="btadd.type"></Cascader>
                </FormItem>
                <FormItem label="新增数量">
                    <InputNumber style="width:100%;" :max="99" :min="1" number v-model="btadd.number"></InputNumber>
                </FormItem>
            </Form>
            <div slot="footer">
                <div class="zh-flex zh-flex-5" >
                    <Button @click='modal_btadd=false' type="ghost">放弃操作</Button>
                    <Button @click='batchAdd' class="zh-hollow" type="primary" style="width:auto;">新增并批量下载二维码</Button>
                </div>
                
            </div>
        </Modal>
        <Modal title="选择机构" v-model="s_tree" @on-ok="confirmST" :mask-closable="false" >
            <div style="padding:15px 30px;">
                <Tree :data="s_tree_data"   @on-select-change="s_selectChange"></Tree>
            </div>
        </Modal>
        <Modal title="选择机构" v-model="f_tree" :loading="ft_load" @on-ok="confirmF" :mask-closable="false" >
			<div style="padding:15px 30px;">
				<Tree :data="f_tree_data"   @on-select-change="f_selectChange"></Tree>
			</div>
		</Modal>
    </div>
</template>

<script>
import {dpwnLoadHref,downQr} from '@/libs/downLoadUrl'
import accounting from 'accounting'
import util from '@/libs/util.js'
import {
    getplEquipTable,
    getBankOrg,
    getDeptList,
    getEquipTable,
    getEquipType,
    getQRcode,
    downloadQR,
    getOrgTree
} from '@/libs/get.js'
import {
    saveEquip,
    deleteEquip,
    plxzEquips
} from '@/libs/post.js'
export default {
    name:'',
    data () {
        return {
            ft_load:true,
			f_select_op:[],
			f_tree_data:[],
			f_tree:false,
            s_select_op:[],
			s_tree_data:[],
			s_tree:false,
            equipUuid:'',
            editInst:'',
            editInstN:'',
            editDept:'',
            user_type:'',
            userUuid:'',
            saveLoad:true,
            btnp:{},
            btn:{
                plxzeng_xzsb:false,
                plxzeng_ewm:false,
                plxzeng_bj:false,
                plxzeng_sc:false
            },
            modal_btadd:false,
            btadd:{
                customer:'',
                mechanism:'',
                mechanismName:'',
                type:[],
                number:1
            },
            modal_deviceInfo:false,
            deviceInfo:{
                more:false,
                uuid:'',
                type:[],
                date:'',
                dateTime:'',
                customerUuid:'',
                customer_name:'',
                inst_name:'',
                instUuid:'',
                part_name:'',
                deptUuid:'',
                bussinesUuid:'',
                typeCdUuid:'',
                name:'',
                eqyipType:'',
                brand:'',
                purchaseDate:'',
                amt:'',
                sn:'',
                assets_code:'',
                receiptor:'',
                receipt_time:'',
                ip:'',
                mac:'',
                facilitator:'',
                facilitator_tel:'',
                remark:''
            },
            modal_qr:false,
            qrcodeinfo:{
                qrcode:'',
                number:'',
                type:[],
                name:''
            },
            loading:true,
            total:0,
            timeRange:[],
            formItem:{
                inst:'全部',
                code:'',
                instName:'',
                type:['全部','全部'],
                customerUuid:'全部',
                startTime:'',
                endTime:'',
                pageNumber:1,
                pageSize:10 
            },
            moreCustomerList:[],
            customerList:[],
            moreMechanismList:[],
            mechanismList:[],
            moreTypeList:[],
            typeList:[] ,
            partList:[] ,
            columns:[
                {
                    key:'code',
                    title:'设备编号',
                    fixed:'left',
                    minWidth:150
                },
                {
                    key:'eqyipType',
                    title:'类型',
                    minWidth:150
                },
                {
                    key:'customerName',
                    title:'所属客户',
                    minWidth:150
                },
                {
                    key:'iname',
                    title:'所属机构',
                    minWidth:150
                },
                {
                    key:'time',
                    title:'新增时间',
                    minWidth:150,
                    align:'center'
                },
                {
                key:'action',
                title:'操作',
                align:'center',
                minWidth:200,
                fixed:'right',
                render:(h,params)=>{
                    //判断是否显示某些操作按钮
                    let uuid=params.row.uuid;
                    let status=params.row.status;
                    return h('div',[
                    h('Button',{
                        props:{
                        type:'text',
                        size:'small'
                        },
                        style:{
                        color:'#2D8CF0',
                        display:this.btnp.plxzeng_ewm?'inline-block':'none'
                        },
                        on:{
                        click:()=>{
                            this.qrcodeinfo={
                                qrcode:params.row.qrcode,
                                number:params.row.code,
                                type:params.row.eqyipType,
                                name:params.row.name
                            };
                            this.modal_qr=true;
                            this.equipUuid=params.row.uuid;
                        }
                        }
                    },'二维码'),
                    h('Button',{
                        props:{
                        type:'text',
                        size:'small'
                        },
                        style:{
                        color:'#2D8CF0',
                        display:this.btnp.plxzeng_bj?'inline-block':'none'
                        },
                        on:{
                        click:()=>{ 
                            this.getRowInfo(params.row);  
                        }
                        }
                    },'编辑'), 
                    h('Button',{
                        props:{
                        type:'text',
                        size:'small'
                        },
                        style:{
                        color:'#EA0000',
                        display:this.btnp.plxzeng_sc?'inline-block':'none'
                        },
                        on:{
                        click:()=>{
                            let config={
                            title:'删除设备',
                            content:'设备删除后不可被恢复。',
                            okText:'删除',
                            cancelText:'放弃操作',
                            onOk:()=>{
                                this.delete(params.row.uuid);
                            }
                            }
                            this.$Modal.confirm(config)
                        }
                        }
                    },'删除')
                    ])
                }
                }
                
            ],
            data:[]
        }
    },
    watch:{
        modal_btadd:function(){
            if(!this.modal_btadd){
                this.customerList=[];
                this.btadd={
                    customer:'',
                    mechanism:'',
                    mechanismName:'',
                    type:[],
                    number:1
                }
            }else{
                if(this.user_type=='01'){
                    //this.newSearchCustomerChange(this.userUuid,'form')
                    this.getFormInstTree(this.userUuid);
                }else{
                    this.customerList=JSON.parse(JSON.stringify(this.$store.state.user.customer));
                }
            }
        },
        modal_deviceInfo:function(){
            if(!this.modal_deviceInfo){
                
                this.resetForm();
                this.editInst='';
                this.editDept='';
                this.editInstN='';
            }else{
                this.customerList=JSON.parse(JSON.stringify(this.$store.state.user.customer));
            }
        }
    },
    created(){
        this.btnp=util.bpower(this,this.btn);
        this.init();
        this.getTable();
        this.getEquipTypeList();
    },
    methods:{
        init(){
            let that=this;
            util.isCustomer(this);
            let timer;
			if(that.user_type!=='01'){
				timer=setInterval(()=>{
					if(that.$store.state.user.customer.length>0){
                        that.moreCustomerList=[{name:'全部',uuid:'全部'},...JSON.parse(JSON.stringify(that.$store.state.user.customer))];
                        that.customerList=JSON.parse(JSON.stringify(that.$store.state.user.customer));
						clearInterval(timer)	
					}
				},500);
			}else{
                this.searchCustomerChange(this.userUuid);	
                //this.newSearchCustomerChange(this.userUuid,'search');	
                //this.newSearchCustomerChange(this.userUuid,'form'); 
                this.getFormInstTree(this.userUuid);
			}
            // setTimeout(()=>{
            //     if(this.user_type!=='01'){
            //         that.moreCustomerList=[{name:'全部',uuid:'全部'},...that.$store.state.user.customer];
            //         that.customerList=that.$store.state.user.customer;
            //     }
            // },5000);
            // if(this.user_type=='01'){   
            //     this.newSearchCustomerChange(this.userUuid,'search');
            //     this.newSearchCustomerChange(this.userUuid,'form'); 
            // } 
        },
        async getFormInstTree(value,p,c,d){
			this.deviceInfo.instUuid='';
            this.deviceInfo.inst_name='';
            
			this.f_select_op=[];
			if(value!==''){
				let res=await getOrgTree(value,p,c,d);
				let response=res.data;
				if(response.code===200){
					this.f_tree_data=util.tree([response.data]);
				}else{
					this.$Notice.error({
						title: '获取机构失败',
						desc:response.msg
					});
				}
			}
		},
		f_selectChange(selectarr){
			this.f_select_op=selectarr;
		},
		async confirmF(){
			if(this.f_select_op.length>0){
				if(this.f_select_op[0].canChoose=='true'){
                    if(this.modal_btadd){
                        this.btadd.mechanism=this.f_select_op[0].value;
                        this.btadd.mechanismName=this.f_select_op[0].title;
                    }else{
                        this.deviceInfo.instUuid=this.f_select_op[0].value;
                        this.deviceInfo.inst_name=this.f_select_op[0].title;
                        await this.changeInst(this.f_select_op[0].value);
                    }
					
				}else{
					this.$Notice.error({
						desc:'请选择有效节点'
					});
					this.ft_load=false;
					let that=this;
					setTimeout(() => {
						that.ft_load=true;
					}, 200);
					return false
				}
				
			}else{
				this.deviceInfo.instUuid='';
                this.deviceInfo.inst_name='';
                this.btadd.mechanism='';
                this.btadd.mechanismName='';
			}
			this.f_tree=false;
		},
        async getSearchInstTree(value){
            this.formItem.inst='';
            this.formItem.instName='';
            this.s_select_op=[];
			if(value!=='全部'){
				let res=await getOrgTree(value);
				let response=res.data;
				if(response.code===200){
					this.s_tree_data=util.tree([response.data]);
				}else{
					this.$Notice.error({
						title: '获取机构失败',
						desc:response.msg
					});
				}
			}
		},
		s_selectChange(selectarr){
			this.s_select_op=selectarr;
		},
		confirmST(){
			if(this.s_select_op.length>0){
				this.formItem.inst=this.s_select_op[0].value;
				this.formItem.instName=this.s_select_op[0].title;
			}else{
				this.formItem.inst='';
				this.formItem.instName='';
			}
		},
        async downQrPic(){
            let name=this.qrcodeinfo.qrcode;
            let uuid=this.equipUuid;
            
            let download=window.open(`${downQr}&uuid=${uuid}&name=${name}`,'_blank');
                let that=this;
                download.onbeforeunload=function(){
                    
                };
        },
        async batchAdd(){
            if(this.user_type=='01'&&this.btadd.customer.length<=0){
                this.$Notice.error({
                    desc:'请选择设备所属客户'
                })
            }else if(this.btadd.mechanism.length<=0){
                this.$Notice.error({
                    desc:'请选择所属机构'
                })
            }else if(this.btadd.type.length<=0||this.btadd.type[0].length<=0){
                this.$Notice.error({
                    desc:'请选择设备父类和子类'
                })
            }else if(this.btadd.number<1){
                this.$Notice.error({
                    desc:'请输入新增数量'
                })
            }else if(this.btadd.number>99){
                this.$Notice.error({
                    desc:'每次最多新增99个同类设备'
                })
            }else{
                let instUuid=this.btadd.mechanism;
                let bussines=this.btadd.type[0];
                let type_cd=this.btadd.type[1];
                let numbers =this.btadd.number;
                let download=window.open(`${dpwnLoadHref}${util.pjArgu({instUuid,bussines,type_cd,numbers})}`,'_blank');
                let that=this;
                that.modal_btadd=false;
                that.btadd.customer='';
                download.onbeforeunload=function(){
                    that.formItem.pageNumber=1;
                    that.getTable();   
                };
                
                that.customerList=[];
               
                
            }
           
        },
        async delete(uuid){
            let res =await deleteEquip(uuid);
            let response=res.data;
            if(response.code==200){
                this.$Notice.success({
                    desc:"已删除"
                });
                this.getTable();
            }else{
                this.$Notice.error({
                    desc:response.msg
                })
            }
        },
        searchGetTable(){
            this.formItem.pageNumber=1;
            this.getTable();
        },
        async getTable(){
            this.loading=true;
            let form=JSON.parse(JSON.stringify(this.formItem));
            for(let key in form){
                if(form[key]==='全部'){
                    form[key]='';
                }
            }
            if(!!this.timeRange[0]&&this.timeRange.length>0){
                form.startTime=util.time1(this.timeRange[0]);
                form.endTime=util.time1(this.timeRange[1]);
            }
            if(form.type.length>0){
                form.bussines=form.type[0]=='全部'?'':form.type[0];
                form.type_cd=form.type[1]=='全部'?'':form.type[1];
            }    
            let res=await getplEquipTable(form);
            let response=res.data;
            if(response.code===200){
                this.total=response.data.total;
                this.data=response.data.rows.map(item=>{
                    item.time=item.created.slice(0,16);
                    return item
                })
                this.loading=false;
            }else{
                this.$Notice.error({
                    desc:response.msg
                });
                
            }
        },
        async save(){
            let form=JSON.parse(JSON.stringify(this.deviceInfo));
            if(this.user_type=='01'){
                form.customerUuid=this.userUuid;
            }
            form.bussinesUuid=form.type[0];
            form.typeCdUuid=form.type[1];
            form.purchaseDate=!!form.date?form.date:'';
            form.receipt_time=!!form.dateTime?form.dateTime:'';
            if(this.partList.length<=0){
                form.deptUuid=''
            }else{
                let arr=this.partList.map(item=>{
                    return item.uuid
                });
                if(!arr.includes(form.deptUuid)){
                    form.deptUuid=''
                }
            }
            let res=await saveEquip(form);
            let response=res.data;
            if(response.code==200){
                this.$Notice.success({
                    desc:"已保存"
                });
                this.formItem.pageNumber=1;
                this.getTable();
                this.modal_deviceInfo=false;
            }else{
                this.$Notice.error({
                    desc:response.msg
                });
            }
        },
        resetForm(){
            this.deviceInfo={
                more:false,
                uuid:'',
                type:[],
                date:[],
                dateTime:[],
                customerUuid:'',
                customer_name:'',
                inst_name:'',
                instUuid:'',
                part_name:'',
                deptUuid:'',
                bussinesUuid:'',
                typeCdUuid:'',
                eqyipType:'',
                name:'',
                brand:'',
                purchaseDate:'',
                amt:'',
                sn:'',
                assets_code:'',
                receiptor:'',
                receipt_time:'',
                ip:'',
                mac:'',
                facilitator:'',
                facilitator_tel:'',
                remark:''
            }
        },
        async getRowInfo(obj){
            this.deviceInfo={
                more:false,
                uuid:obj.uuid,
                type:[obj.bussines_uuid,obj.type_cd_uuid],
                customerUuid:obj.customerUuid,
                customer_name:obj.customerName,
                inst_name:obj.iname,
                instUuid:obj.inst,
                part_name:obj.departmentName,
                deptUuid:obj.department,
                bussinesUuid:obj.bussines_uuid,
                typeCdUuid:obj.type_cd_uuid,
                eqyipType:obj.eqyipType,
                name:!!obj.name?obj.name:'',
                brand:obj.brand,
                amt:obj.amt,
                sn:obj.sn,
                assets_code:obj.assets_code,
                receiptor:obj.receiptor,
                ip:obj.ip,
                mac:obj.mac,
                facilitator:obj.facilitator,
                facilitator_tel:obj.facilitator_tel,
                remark:obj.remark,
                purchaseDate:!!obj.purchase_date?obj.purchase_date:'',
                receipt_time:!!obj.receipt_time?obj.receipt_time:'',
                date:!!obj.purchase_date?obj.purchase_date:'',
                dateTime:!!obj.receipt_time?obj.receipt_time:'',
            };
            this.editInstN=obj.iname;
            this.editInst=obj.inst;
            this.editDept=obj.department;
            if(this.infotype=='view'){
                this.deviceInfo.amt=accounting.formatMoney(accounting.unformat(this.deviceInfo.amt),'',2,',')
            }else{
                //await this.newSearchCustomerChange(obj.customerUuid,'form');
                await this.getFormInstTree(obj.customerUuid);
                this.f_tree_data=util.TreeFormat2(this.f_tree_data,this.editInst);
                this.deviceInfo.instUuid=this.editInst;
                this.deviceInfo.inst_name=this.editInstN;
                if(!!obj.inst||obj.inst.length>0){
                    await this.changeInst(obj.inst);
                    this.deviceInfo.deptUuid= this.editDept;
                }
                this.modal_deviceInfo=true;
            }
        },
        amtChange(e){
            let amt=util.amount(this.deviceInfo.amt);
            this.$set(this.deviceInfo,'amt',amt);
          
        },
        async changeInst(value){
            this.deviceInfo.part='';
            let res =await getDeptList(value);
            let response=res.data;
            if(response.code==200){
                this.partList=response.data;
            }else{
                this.$Notice.error({
                    desc:response.msg
                })
            }
        },
        async validate(){
            if(this.user_type!='01'&&this.deviceInfo.customerUuid.length<=0){
                this.$Notice.error({
                    desc:"请选择设备所属客户"
                })
            }else if(this.deviceInfo.instUuid.length<=0){
                this.$Notice.error({
                    desc:"请选择所属机构"
                })
            }else if(this.deviceInfo.type.length<=0||this.deviceInfo.type[0].length<0){
                this.$Notice.error({
                    desc:"请选择设备父类和子类"
                })
            }else if(this.deviceInfo.name.length<=0){
                this.$Notice.error({
                    desc:"请输入设备名称"
                })
            }else if(this.deviceInfo.name.length<=2){
                this.$Notice.error({
                    desc:"设备名称须大于2个字符"
                })
            }else{
                //save
                await this.save();
            }
            let that=this;
            this.saveLoad=false;
            setTimeout(()=>{
                that.saveLoad=true;
            },500)
        },
        changeKey(arr){
            let newArea = arr.map(item => {
                let a = {};
                a.label = item.name;
                a.value = item.uuid;
                if (!!item.children && item.children.length > 0) {
                    a.children = this.changeKey(item.children);

                }
                return a

            })
            return newArea
        },
        pageSizeChange(size){
            //回到第一页
            this.formItem.pageNumber=1;
            this.formItem.pageSize=size;
            this.getTable();
        },
        pageChange(number){
            this.formItem.pageNumber=number;
            this.getTable();
        },
        changeDate(date){
            this.deviceInfo.date=date;
        },
        changeDateTime(dateTime){
            this.deviceInfo.dateTime=dateTime;
        },
        async formCustomerChange(value){
                //this.newSearchCustomerChange(this.deviceInfo.customerUuid,'form');
                await this.getFormInstTree(this.deviceInfo.customerUuid);
                this.deviceInfo.inst_name=this.editInstN;
                this.deviceInfo.instUuid=this.editInst;
                this.deviceInfo.deptUuid= this.editDept;
           
        },
        async formCustomerChange2(value){
                //this.newSearchCustomerChange(this.btadd.customer,'form');
                await this.getFormInstTree(this.btadd.customer);
                this.btadd.mechanism='';
                this.btadd.mechanismName='';    
        },
        searchCustomerChange(value){
            this.getSearchInstTree(value);
            //this.newSearchCustomerChange(value,'search')
        },
        async newSearchCustomerChange(value,type){
            if(value!=='全部'){
            let res =await getBankOrg(value,'','','')
            if(res.status===200){
                let response=res.data;
                if(response.code===200){
                    if(type==='search'){
                        this.moreMechanismList=[{uuid:'全部',name:'全部'},...response.data]
                    }else if(type==='form'){
                        this.mechanismList=response.data
                    }
                }else{
                    this.$Notice.error({
                        title: '请求出错',
                        desc:response.msg
                    });
                }
            }else{
                this.$Notice.error({
                    title: '请求失败'
                });
            } 
            }else{
                this.formItem.inst='';
            }
        },
        async getEquipTypeList(){
            let res =await getEquipType();
            let response=res.data;
            if(response.code==200){
                this.typeList=this.changeKey(response.data);
                this.moreTypeList=[{value:'全部',label:'全部',children:[{value:'全部',label:'全部'}]},...JSON.parse(JSON.stringify(this.typeList)).map(item=>{
                    if(!!item.children && item.children.length > 0){
                        item.children=[{value:'全部',value:'全部'},...item.children]
                    }
                    return item
                })];
            }else{
                this.$Notice.error({
                    desc:response.msg
                })
            }
        }
    }
}


</script>
<style lang='scss' >
.ivu-modal-footer>div{
    width:100%;
}
</style>